#include "sessionfriendarea.h"

SessionFriendArea::SessionFriendArea()
{
    // 1. 设置基础属性
    // ⼀定要添加这个设置, 否则⽆法正确显⽰.
    this->setWidgetResizable(true);
// 设置滚动条宽度
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width:2px; background-color: rgb(46, 46, 46); }");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0px;}");

    // 2. 创建核⼼ Widget
    container = new QWidget(this);
    container->setFixedWidth(310);
    this->setWidget(container);

    // 3. 创建布局
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);
    container->setLayout(layout);


}

void SessionFriendArea::clear()
{
    QLayout* layout = container->layout();
    // 从后往前遍历. 否则删除前⾯的元素会影响后⾯元素的下标
    for (int i = layout->count() - 1; i >= 0; i--) {
        QLayoutItem* item = layout->takeAt(i);
        if (item->widget()) {
            delete item->widget();
        }
        delete item;
    }

}

void SessionFriendArea::addItem(ItemType itemType, const QString &id, const QIcon &avatar, const QString &name, const QString &text)
{
    SessionFriendItem* itemWidget = nullptr;
    if (itemType == FriendItemType) {
        itemWidget = new FriendItem(this, id, avatar, name, text);
    } else if (itemType == SessionItemType) {
        itemWidget = new SessionItem(this, id, avatar, name, text);
    } else if (itemType == ApplyItemType) {
        itemWidget = new ApplyItem(this, id, avatar, name);
    }
    // 添加到 container 中
    container->layout()->addWidget(itemWidget);
}

void SessionFriendArea::clickItem(int index)
{
    if (index >= container->layout()->count()) {
        qCritical() << TAG << "点击的元素下标超出范围! index=" << index;
        return;
    }
    QLayoutItem* top = container->layout()->itemAt(index);
    if (top->widget() == nullptr) {
        qCritical() << TAG << "指定元素不存在! index=" << index;
        return;
    }
    SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(top->widget());
    item->select();
}



SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const
                                     QString &name, const QString &lastMessage)
    : owner(owner)
{
    this->selected = false;
    this->setStyleSheet("QWidget {background-color: rgb(231, 230, 229);}");
    this->setFixedHeight(70); //设置后高度不变，固定高度

    layout=new QGridLayout();
    layout->setContentsMargins(20, 0, 0, 0);
    layout->setHorizontalSpacing(10);

    layout->setVerticalSpacing(0);
    this->setLayout(layout);

    // 创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIcon(avatar);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setStyleSheet("QPushButton {background-color: rgb(231, 230,229);}");
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 创建名字
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(name);
    nameLabel->setStyleSheet("QLabel { font-size: 18px; font-weight: 600; }");
    nameLabel->setAlignment(Qt::AlignBottom); // 调整下⽂字对⻬位置, 看起来更好看⼀些.

    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 创建消息预览
    messageLabel = new QLabel();
    messageLabel->setText(lastMessage);
    messageLabel->setFixedHeight(35);
    messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    layout->addWidget(avatarBtn, 0, 0, 2, 2);
    layout->addWidget(nameLabel, 0, 2, 1, 8);
    layout->addWidget(messageLabel, 1, 2, 1, 8);
}

void SessionFriendItem::mousePressEvent(QMouseEvent *event)
{
    (void) event;
    select();
}

void SessionFriendItem::select()
{
    // 清空其他兄弟元素的样式
    const QObjectList& children = this->parentWidget()->children();

    for (QObject* child : children) {
        if (!child->isWidgetType()) {
            // 不是 QWidget 直接跳过.
            continue;
        }
        SessionFriendItem* widget = dynamic_cast<SessionFriendItem*>(child);
        widget->selected = false;
        widget->setStyleSheet("QWidget {background-color: rgb(231, 230,229);}");
    }

    // 应⽤⾃⼰的样式.
    this->setStyleSheet("QWidget { background-color: rgb(210, 209, 209); }");
    this->selected = true;
    // TODO 进⼀步的加载详情. 按照多态的⽅式调⽤到对应⼦类的 active
    this->active();
}

void SessionFriendItem::enterEvent(QEnterEvent *event)
{
    (void) event;
    if (!this->selected) {
    this->setStyleSheet("QWidget { background-color: rgb(215, 215, 215);}");}
}

void SessionFriendItem::leaveEvent(QEvent *event)
{
    (void) event;
    if (!this->selected) {
        this->setStyleSheet("QWidget {background-color: rgb(231, 230, 229);}");
    }
}

void SessionFriendItem::paintEvent(QPaintEvent *event)
{
    // 这⾥是官⽅⽂档给出的要求. 原因在⽂档上没有解释.
    (void) event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

SessionItem::SessionItem(QWidget* owner, const QString &chatSessionId, const
                         QIcon &avatar, const QString &name, const QString &lastMessage)
    : SessionFriendItem(owner, avatar, name, lastMessage),
    chatSessionId(chatSessionId)
{ }
void SessionItem::active()
{
    LOG() << "SessionItem active. chatSessionId=" << chatSessionId;
    // TODO
}

FriendItem::FriendItem(QWidget* owner, const QString &friendId, const QIcon
                                                                    &avatar, const QString &name, const QString &lastMessage)
    : SessionFriendItem(owner, avatar, name, lastMessage), userId(friendId)
{ }
void FriendItem::active()
{
    LOG() << "FriendItem active. userId=" << userId;
    // TODO
}

ApplyItem::ApplyItem(QWidget *owner, const QString& userId, const QIcon&
                                                                avatar, const QString& name)
    : SessionFriendItem(owner, avatar, name, ""), userId(userId)
{
    // 1. 移除 messageLabel
    layout->removeWidget(messageLabel);
    messageLabel->deleteLater();
    // 2. 创建两个按钮
    QPushButton* acceptBtn = new QPushButton();
    acceptBtn->setText("同意");
    QPushButton* rejectBtn = new QPushButton();
    rejectBtn->setText("拒绝");
    // 3. 添加到布局中
    // layout->addWidget(messageLabel, 1, 2, 1, 8);
    layout->addWidget(acceptBtn, 1, 2, 1, 2);
    layout->addWidget(rejectBtn, 1, 4, 1, 2);
}
void ApplyItem::active()
{
    LOG() << "ApplyItem active.";
    // 这⾥不需要做额外的操作, 空函数即可.
}

